package com.edu.test;

import com.edu.bean.Emp;
import com.edu.mapper.EmpMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @Author:何包蛋
 * @Date :2022/10/9 - 10 - 09 - 10:46
 */
public class test01 {


    private SqlSession sqlSession;

    @Before
    public void init() throws Exception{
        String path = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(path);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sqlSessionFactory.openSession();
    }


    @Test
    public void test01(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = new Emp("张三","技术部",25,"男");
        Emp emp2 = new Emp("李四","技术部",35,"男");
        Emp emp3 = new Emp("王二","财务部",33,"男");
        Emp emp4 = new Emp("淑芬","行政部",21,"女");
        Emp emp5 = new Emp("翠花","行政部",24,"女");

        empMapper.insertEmp(emp);
        empMapper.insertEmp(emp2);
        empMapper.insertEmp(emp3);
        empMapper.insertEmp(emp4);
        empMapper.insertEmp(emp5);

        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test02(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);

        empMapper.deleteEmp(3);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test03(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        empMapper.updateEmpAge(2,36);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test04(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        System.out.println(empMapper.selectEmpById(1));
    }

    @Test
    public void test05(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        // 并不是用来 表示一个 员工，只是用来装 查询的一些条件
//        Emp emp = new Emp();
//        emp.setEmpDepart("技术部");
//        emp.setEmpSex("男");
//        List<Emp> emps = empMapper.selectEmp2(emp);
//        for(Emp e:emps){
//            System.out.println(e);
//        }

    }

    @Test
    public void test06(){

        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);

        List<Emp> emps = empMapper.selectEmpByAge(20,30);
        for(Emp e:emps){
            System.out.println(e);
        }

    }

    @Test
    public void test07(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
//        Emp emp = new Emp();
//        emp.setEmpDepart("行政部");
//        List<Emp> emps = empMapper.selectEmps(10,100,emp);
//        for(Emp e:emps){
//            System.out.println(e);
//        }

    }

    @Test
    public void test08(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Map<String, Object> map = empMapper.selectEmpById2(1);
        System.out.println(map);
        System.out.println(map.get("empId"));
    }

    @Test
    public void test09(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);

        List<Emp> emps = empMapper.selectAllEmp();

        for(Emp e: emps){
            System.out.println(e);
        }
    }

    @Test
    public void test10(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);

        List<Map<String, Object>> maps = empMapper.selectAllEmp2();

        for(Map<String,Object> map: maps){
            System.out.println(map);
        }
    }

    @Test
    public void test11(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);

        System.out.println(empMapper.selectEmpNum());
    }

    @Test
    public void test12(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        System.out.println(empMapper.selectAvgAge());
    }
}
